package com.xb.p4_widgets;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class ToastActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);

        findViewById(R.id.btn_simple).setOnClickListener(this);
        findViewById(R.id.btn_location).setOnClickListener(this);
        findViewById(R.id.btn_image).setOnClickListener(this);
        findViewById(R.id.btn_custom).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        Toast toast=null;
        switch(v.getId()){
            case R.id.btn_simple:
                Toast.makeText(getApplicationContext(),"默认的Toast",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_location:
                toast=Toast.makeText(getApplicationContext(),"自定义位置Toast",Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER,0,0);
                toast.show();
                break;
            case R.id.btn_image:
                toast=Toast.makeText(getApplicationContext(),"带图片的Toast",Toast.LENGTH_LONG);

                LinearLayout toastLayout=(LinearLayout) toast.getView();
                ImageView imageCode=new ImageView(getApplicationContext());
                imageCode.setImageResource(R.drawable.ieboot);
                toastLayout.addView(imageCode);

                toast.setGravity(Gravity.CENTER,0,0);
                toast.show();
                break;
            case R.id.btn_custom:

                //获取布局文件的填充器
                LayoutInflater inflater =getLayoutInflater();
                View layout=inflater.inflate(R.layout.custom_toast,(ViewGroup)findViewById(R.id.custom_layout));

                TextView title=layout.findViewById(R.id.toast_title);
                title.setText("完全自定义的Toast");

                ImageView imageView=layout.findViewById(R.id.image_toast);
                imageView.setImageResource(R.drawable.ieboot);

                TextView text=layout.findViewById(R.id.tv_toast);
                text.setText("这是一个完全自定义的Toast的描述信息");

                toast=new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER|Gravity.TOP,5,5);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
                break;

        }

    }
}
